<?php 
namespace app\user\controller;

use think\Controller;
use think\Request;
use think\Session;
use app\common\traits\controller\TraitController;
/**
 * 会员心愿类
 */
class Wish extends Controller
{
    use TraitController;
	
	public function init(){
		if (!model('Users')->checkLogin()) {
			Session::set('redirect_url', Request::instance()->url(true));
            $this->redirect('@user/login');
            exit;
        }
	}
	/**
     * [FunctionName 城市列表数组]
     * @param string $value [description]
     */
    public function getHotCity()
    {
        $city = ['1'=>'北京','2'=>'上海','3'=>'三亚','4'=>'成都','5'=>'西安'];
        return $city;
    }
	
	/**
	 * 个人中心心愿列表
	 */
	public function getIndex(){
		$list = model('Wishs')->getWishList();
		return view('wish/mywishlist',['list'=>$list]);
	}
	
	/**
	 * 添加心源表单
	 */
	public function getCreate(){
		$city = $this->getHotCity();
		return view('wish/publishwish',['city'=>$city]);
	}
	
	/**
	 * 添加心源
	 */
	public function postSave(Request $request){
		if(!$request->post('city') || !$request->post('start_date') || !$request->post('end_date')){
			return json(['code'=>-100,'msg'=>'请选择出行目的地和起止日期']);
		}
		if($request->post('city')){
			$address_info['city'] = $request->post('city');
		}else{
			$address_info['city']=-1;
		}
		if(model('Wishs')->createWish(array_merge($request->post(),$address_info))){
			return json(['code'=>1,'msg'=>lang('add wish success')]);
		}
		return json(model('Wishs')->getError());
	}
	
	/**
	 * 编辑心愿表单
	 */
	public function getEdit(){
		$data = model('Wishs')->where(['id'=>input('get.id/d'),'user_id'=>self::getLoginUserInfo('id')])->find();
		$city = $this->getHotCity();
	    return view('wish/updatewish',['data'=>$data,'city'=>$city]);
	}
	/**
	 * 编辑心愿
	 * @param $where 条件
	 */
	public function postUpdate(Request $request){
		if(!$request->post('city') || !$request->post('start_date') || !$request->post('end_date')){
			return json(['code'=>-100,'msg'=>'请选择出行目的地和起止日期']);
		}
		if($request->post('city')){
			$address_info['city'] = $request->post('city');
		}else{
			$address_info['city']=-1;
		}
	   	if(model('Wishs')->updateWish(input('post.id/d'),array_merge($request->post(),$address_info))){
	   		return json(['code'=>1,'msg'=>lang('edit wish success')]);
	  	}
	    return json(model('Wishs')->getError());
	}
	
	/**
	 * 取消发布心愿
	 */
	public function getCancelWish(){
		if(model('Wishs')->cancelWish(input('get.'))){
			$this->success(lang('cancel wish success'));
		}
		$this->error(lang('cancel wish error'));
	}
	
	/**
	 * 心愿接单房车列表
	 */
	public function getAcceptOrderCaravan(){
		$wishId = input('get.id');
		$data = model('Wishs')->where(['id'=>$wishId,'user_id'=>self::getLoginUserInfo('id')])->find();
		$wishUsers = model('WishUsers')->where('wish_id',$data['id'])->select();
		if(!$wishUsers){
			$this->error('参数错误');
		}
		$caravanId = model('WishUsers')->where('wish_id',$wishId)->column('caravan_id');
		//房车信息
		$carvanData = model('Caravans')->where('id','in',$caravanId)->select();
	    return view('wish/acceptordercaravan',['carvanData'=>$carvanData,'wishId'=>$wishId]);
	}
	
	/**
	 * 全部心愿列表
	 */
	public function getAllWishList(){
		$list = model('Wishs')->getWishsList(input('get.start_date'),input('get.end_date'),1);
	    return view('wish/wishlist',['list'=>$list]);
	}	
	
	/**
	 * ajax获取更多心愿
	 */
	public function postGetManyWish(){
		return json(model('Wishs')->getWishsList(input('post.start_date'),input('post.end_date'),$page=input('post.page')));
	}
	/**
	 * 接单
	 */
	public function postAcceptOrder(){
		//$user_id = model('Wishs')->where('id',input('id'))->value('user_id');
		if(model('Wishs')->meetTheDemand(input('id'))){
			//model('Messages')->createMsg($msg_info = ['users_id' => $user_id, 'body' => '恭喜您,您发布的心愿需求,有人接单了']);
			$this->success(lang('order receiving success'),'/wishs/myacceptorder');
		}
		$this->error(model('Wishs')->getError());
	}
	
	/**
	 * 我的接单列表
	 */
	public function getMyAcceptOrder(){
		$list = model('WishUsers')->where('meet_user_id',self::getLoginUserInfo('id'))->select();
	    return view('wish/myacceptorder',['list'=>$list]);
	}
	
	/**
	 * 取消我的接单
	 */
	public function getCancelOrder(){
	   if(model('Wishs')->cancelMeetTheDemand(input('get.id'))){
	   	$this->success(lang('cancel my Order success'));
	   }
	   $this->error('cancel my order error');
	}
	
	/**
	 * 地区插件
	 */
    public function getCountry()
    {
        $country = db('area')->where('type',0)->field(['area_code','parent_code','area_name','type'])->select();
        return json($country);
        exit;
    }
    public function getProvince()
    {
        $addressProvince = input('addressCountry');
        $province = db('area')->where('type',1)->where('parent_code',$addressProvince)->field(['area_code','parent_code','area_name','type'])->select();
        return  json($province);
    }
    public function getCity()
    {
        $city = input('addressProvince');
        $city = db('area')->where('type',2)->where('parent_code',$city)->field(['area_code','parent_code','area_name','type'])->select();
        return json($city);
    }
}



 ?>